"""
金融数据平台配置文件
"""
import os
from pathlib import Path
from dotenv import load_dotenv

# 加载环境变量
load_dotenv()

# 基础路径配置
BASE_DIR = Path(__file__).resolve().parent.parent
LOG_DIR = BASE_DIR / "logs"
LOG_DIR.mkdir(exist_ok=True)

# 数据库配置
DATABASE = {
    "host": os.getenv("DB_HOST", "localhost"),
    "port": os.getenv("DB_PORT", "5432"),
    "user": os.getenv("DB_USER", "postgres"),
    "password": os.getenv("DB_PASSWORD", "postgres"),
    "database": os.getenv("DB_NAME", "finance_data"),
}
DATABASE_URL = f"postgresql://{DATABASE['user']}:{DATABASE['password']}@{DATABASE['host']}:{DATABASE['port']}/{DATABASE['database']}"

# Celery配置
RABBITMQ = {
    "host": os.getenv("RABBITMQ_HOST", "localhost"),
    "port": os.getenv("RABBITMQ_PORT", "5672"),
    "user": os.getenv("RABBITMQ_USER", "guest"),
    "password": os.getenv("RABBITMQ_PASSWORD", "guest"),
    "vhost": os.getenv("RABBITMQ_VHOST", "/"),
}
CELERY_BROKER_URL = f"amqp://{RABBITMQ['user']}:{RABBITMQ['password']}@{RABBITMQ['host']}:{RABBITMQ['port']}/{RABBITMQ['vhost']}"

REDIS = {
    "host": os.getenv("REDIS_HOST", "localhost"),
    "port": os.getenv("REDIS_PORT", "6379"),
    "password": os.getenv("REDIS_PASSWORD", ""),
    "db": os.getenv("REDIS_DB", "0"),
}
CELERY_RESULT_BACKEND = f"redis://:{REDIS['password']}@{REDIS['host']}:{REDIS['port']}/{REDIS['db']}" if REDIS['password'] else f"redis://{REDIS['host']}:{REDIS['port']}/{REDIS['db']}"

# API配置
API_HOST = os.getenv("API_HOST", "0.0.0.0")
API_PORT = int(os.getenv("API_PORT", "8000"))
SECRET_KEY = os.getenv("SECRET_KEY", "your-secret-key-for-jwt")
ACCESS_TOKEN_EXPIRE_MINUTES = int(os.getenv("ACCESS_TOKEN_EXPIRE_MINUTES", "1440"))  # 24小时

# 日志配置
LOG_LEVEL = os.getenv("LOG_LEVEL", "INFO")
LOG_FORMAT = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"

# 数据采集配置
DATA_COLLECTION = {
    "retry_limit": int(os.getenv("RETRY_LIMIT", "3")),
    "retry_delay": int(os.getenv("RETRY_DELAY", "60")),  # 秒
    "batch_size": int(os.getenv("BATCH_SIZE", "100")),
    "concurrency": int(os.getenv("CONCURRENCY", "4")),
    "api_rate_limit": int(os.getenv("API_RATE_LIMIT", "30")),  # 每分钟请求数
}